package com.lkl;

import cn.hutool.core.date.DateTime;
import com.lkl.dao.AddressDao;
import com.lkl.entity.Address;
import com.lkl.entity.AddressVO;
import com.lkl.utils.DataNewUtil;
import com.lkl.utils.DataOldUtil;
import com.lkl.utils.FileUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

public class Menu {
    private static final AddressDao addressDao = new AddressDao();

    /**
     * 控制台页面生成JSON数据
     */
    public static void menu() {
        System.out.println(">>>> 获取全国行政区数据");
        System.out.println(">>>> 说明：本系统使用的数据为已爬取的数据，如需重新爬取，请运行JobProcessor.main()函数");
        System.out.println("开始输入条件！！！！");
        System.out.println("地区编码数据：");
        Condition condition = new Condition();
        System.out.printf("-%4s、%s\n", "编码", "名称");
        Condition.provinceMap.forEach((k, v) -> {
            System.out.printf("-%4s、%s\n", k, v);
        });
        List<Object> conditionList = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        System.out.println("1.请输入地区条件：");
        boolean flag = true;
        while (flag) {
            String s = scanner.nextLine();
            if (Condition.provinceMap.containsKey(s)) {
                //设置地区编码条件
                if (!Condition.provinceMap.get(s).equals("全国")) {
                    condition.setProvince(s);
                    conditionList.add(s);
                }
                flag = false;
            } else {
                System.err.println("请输入合理的数据：");
            }
        }
        System.out.printf("-%4s、%s\n", "等级", "条件");
        Condition.levelOneMap.forEach((k, v) -> {
            System.out.printf("-%4s、%s\n", k, v);
        });
        System.out.println("2.请输入等级：");
        flag = true;
        while (flag) {
            String s = scanner.nextLine();
            if (Condition.levelOneMap.containsKey(s)) {
                condition.setLevel(s);
                if ("三级".equals(s)) {
                    conditionList.add(Condition.levelOneMap.get(s));
                }
                flag = false;
            } else {
                System.err.println("请输入合理的数据：");
            }
        }
        StringBuilder sql = new StringBuilder();
        sql.append("select `code`,`name`,`parent_code` from `address` ");
        List<String> sqlArgs = new ArrayList<>();
        if (null != condition.getProvince()) {
            sqlArgs.add(" and `code` like concat (?,'%')");
        }
        if (null != condition.getLevel() && !"''".equals(condition.getLevel())) {
            sqlArgs.add(" and `code` like concat('%',?)");
        }
        if (sqlArgs.size() > 0) {
            sql.append(" where");
            sql.append(sqlArgs.get(0).replace("and", ""));
            if (sqlArgs.size() > 1) {
                sql.append(sqlArgs.get(1));
            }
            sql.append(";");
        }
        System.out.println("====》" + condition);
        System.out.println("====》" + conditionList);
        AddressDao addressDao = new AddressDao();
        List<Address> query = addressDao.query(sql.toString(), conditionList.toArray());
        System.out.println("<==== 查询到的数据条数：" + query.size());
        if (query.size() > 0)
            DataOldUtil.toTreeJson(query, condition);
    }

    /**
     * 批量生成省级以下的Json数据
     */
    private static void extracted() {
        //设置文件命名数据
        Condition condition = new Condition();
        //设置sql语句条件
        List<String> conditionList = new ArrayList<>();
        //实例化dao
        Condition.provinceMap.forEach((k, v) -> {
            //设置省份
            condition.setProvince(v);
            conditionList.add(k);
            Condition.levelOneMap.forEach((k1, v1) -> {
                //设置等级
                condition.setLevel(k1);
                //三级，加上条件
                if ("三级".equals(k1)) {
                    conditionList.add(v1);
                }
                StringBuilder sql = new StringBuilder();
                sql.append("select `code`,`name`,`parent_code` from `address` where `code` like concat (?,'%')");
                if (conditionList.size() > 1) {
                    sql.append(" and `code` like concat('%',?)");
                }
                sql.append(";");
                System.out.println("====》" + condition);
                System.out.println("====》" + conditionList);
                System.out.println("====》" + sql);
                List<Address> query = addressDao.query(sql.toString(), conditionList.toArray());
                System.out.println("<==== 查询到的数据条数：" + query.size());
                if (query.size() > 0) {
                    DataOldUtil.toTreeJson(query, condition);
                }
                conditionList.remove(conditionList.size() - 1);
            });
            conditionList.clear();
        });

    }

    public static boolean continuaFlag = false;

    /**
     * 批量生成全国Json数据
     */
    private static void allJSON() {
        HashMap<String, String> conditionMap = new HashMap<>();
        //定义查询条件
        conditionMap.put("三级",
                "SELECT `code`,`name`,`parent_code` from address WHERE `code` like '%000000';");
        conditionMap.put("四级",
                "SELECT `code`,`name`,`parent_code` from address WHERE `code` like '%000' and `code` not like '%000000';");
        conditionMap.put("五级",
                "select `code`,`name`,`parent_code` from address where `code` not like '%000' ");
        conditionMap.forEach((k,v) -> {
            continuaFlag = false;
            if ("三级".equals(k)) {
                System.out.println("====》 开始查询全国三级数据");
                List<Address> query = addressDao.query(v);
                System.out.println("《==== 查询全国三级数据结束，数据条数：" + query.size());
                if ( query.size() > 0)
                  DataNewUtil.toTreeJsonBy3(query);
            } else if ("四级".equals(k)) {
                System.out.println("====》 开始查询乡级数据");
                List<Address> query = addressDao.query(v);
                System.out.println("《==== 查询乡级数据结束，数据条数：" + query.size());
                if (query.size() > 0)
                  DataNewUtil.toTreeJsonBy4(query);
            }
            else {
                System.out.println("====》 开始查询村级数据");
                List<Address> query = addressDao.query(v);
                System.out.println("《==== 查询村级数据结束，数据条数：" + query.size());
                if (query.size() > 0)
                    DataNewUtil.toTreeJsonBy5(query);
            }
            while (!continuaFlag) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }

    public static void main(String[] args) {
        DateTime start = new DateTime();
        System.out.println("====> 系统开始运行，开始时间：" + start);
        allJSON();
        DateTime end = new DateTime();
        System.out.println("====> 系统结束运行，结束时间：" + end);
        System.out.println("====> 运行时间：" + end.between(start));
    }
}
